package com.ep.thirdPlatformProject.maycur.helper;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.ep.thirdPlatformProject.maycur.entity.loan.MayCurLoanBillRequestParam;
import com.ep.thirdPlatformProject.maycur.entity.loan.MayCurLoanBillResponseDetail;
import com.ep.thirdPlatformProject.maycur.entity.loan.MayCurLoanBillResponseMain;
import com.ep.thirdPlatformProject.maycur.enums.MayCurApiEnum;
import jakarta.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.*;

import static com.ep.thirdPlatformProject.maycur.common.Constant.COMPLETED;


/**
 * 每刻借款单
 *
 * @author Karl
 * @date 2024/03/29
 */

@Component
@Slf4j
public class MayCurLoanHelper {


    public MayCurLoanHelper(MayCurCommonHelper commonHelper,
                            MayCurLoginHelper loginHelper, ThreadPoolTaskExecutor executor
    ) {
        this.commonHelper = commonHelper;
        this.loginHelper = loginHelper;
        this.executor = executor;
    }

    private final MayCurCommonHelper commonHelper;

    private final MayCurLoginHelper loginHelper;

    private final ThreadPoolTaskExecutor executor;


    /**
     * 获取借款单金额汇总
     *
     * @return {@link Map}<{@link String}, {@link BigDecimal}>
     */
    public Map<String, BigDecimal> getLoanAmount(DateTime dateTime) {
        List<MayCurLoanBillResponseDetail> loanList = getLoanList(dateTime);
        return commonHelper.getCommonListAmount(loanList, "借款单", dateTime, executor);
    }


    /**
     * 获取借款单列表
     *
     * @param dateTime 日期时间
     * @return {@link List}<{@link MayCurLoanBillResponseDetail}>
     */
    public List<MayCurLoanBillResponseDetail> getLoanList(@NotNull DateTime dateTime) {
        try {

            String url = commonHelper.constructUrl(MayCurApiEnum.GET_LOAN.getUrl());

            return commonHelper.parseResponseCollecttionToList(
                    MayCurLoanBillResponseMain.class, url,
                    loginHelper::login,
                    pageNo -> buildRequestParam(dateTime, pageNo)
            );

        } catch (MayCurLoginHelper.TokenAcquisitionException ex) {
            log.error("token获取异常", ex);
            throw new MayCurLoginHelper.TokenAcquisitionException("登录异常", ex);
        } catch (Exception ex) {
            log.error("获取报销单列表失败!", ex);
            throw new LoanAcquisitionException("获取报销单列表失败!", ex);
        }
    }


    /**
     * 建立参数
     *
     * @param dateTime 日期时间
     * @param pageNo   页面没有
     * @return {@link MayCurLoanBillRequestParam}
     */
    private MayCurLoanBillRequestParam buildRequestParam(@NotNull DateTime dateTime, Integer pageNo) {
        // 获取本年开始时间
        long yearStartTime = DateUtil.beginOfYear(dateTime).getTime();
        // 获取本年结束时间
        long yearEndTime =  DateUtil.date(dateTime).getTime();
        // 构建参数，获取本年数据
        MayCurLoanBillRequestParam param = new MayCurLoanBillRequestParam()
                .formStatus(COMPLETED)
                .pageNo(pageNo)
                .procInstEndTimeStart(yearStartTime)
                .procInstEndTimeEnd(yearEndTime);
        log.info("请求参数:{}", param);

        return param;


    }

    static class LoanAcquisitionException extends RuntimeException {
        public LoanAcquisitionException(String message) {
            super(message);
        }

        public LoanAcquisitionException(String message, Throwable cause) {
            super(message, cause);
        }
    }

}
